<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
			String basePath = request.getScheme() + "://"
					+ request.getServerName() + ":" + request.getServerPort()
					+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitiona/EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP starting page</title>
<link rel="stylesheet" type="text/css"
	href="js/ext/resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css" href="style/mystyle.css" />
<script type="text/javascript" src="js/ext/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="js/ext/ext-all.js"></script>
<script>
	Ext
			.onReady(function() {

				var a_username = "null";
				var a_password = "null";
				var a_url = "null";
				var a_driver = "null";
				var a_schemaname = "null";
				var a_tablename = "null";

				var window = "null";

				////////////////////////////////////////////////////////////////////////////////// migration detail panel

				var w_3 = new Ext.Panel({
					id : 'w_3',
					region : 'center',
					frame : true,
					margins : '5 5 5 5',
					title : '控制台输出',
					items:[new Ext.form.TextArea({
						height:300,
						width:600,
						id:'w_3_html'
					})]
				});

				setInterval(
						function() {
							// 					dataStore.reload(); // dataStore 换成你的 store 的变量名

							var requestConfig = {
								url : 'importmonitor.svt?kind=importconsole',
								params : {
									username : a_username,
									password : a_password,
									url : a_url,
									driver : a_driver,
									tablename : a_tablename
								},
								callback : function(option, success, response) {
									var msg = [success, option.customer,
											response.responseText];
									Ext.getDom("w_3_html").innerHTML = response.responseText
								}
							}
							Ext.Ajax.request(requestConfig);
						}, 1000);

				var proxy_4 = new Ext.data.HttpProxy({
					url : 'datamigration.svt?kind=migrating'
				});

				var reader_4 = new Ext.data.JsonReader({}, [{
					name : 'tablename'
				}, {
					name : 'driver'
				}, {
					name : 'url'
				}, {
					name : 'username'
				}, {
					name : 'password'
				}, {
					name : 'progress'
				}]);

				var store_4 = new Ext.data.Store({
					proxy : proxy_4,
					reader : reader_4
				});

				store_4.load();

				var w_4 = new Ext.grid.GridPanel({
					store : store_4,
					region : 'east',
					frame : true,
					width : 550,
					title : '迁移任务',
					margins : '5 5 5 0',
					columns : [
					{
						id : 'Table Name',
						width : 150,
						header : '表名',
						sortable : true,
						dataIndex : 'tablename',
						align : 'center'
					}, {
						id : 'Driver',
						header : '驱动',
						width : 150,
						sortable : true,
						dataIndex : 'driver',
						align : 'center'
					}, {
						id : 'Database',
						header : '数据库',
						sortable : true,
						width : 220,
						dataIndex : 'url',
						align : 'center'
					}, {
						id : 'User',
						header : '用户',
						sortable : true,
						dataIndex : 'username',
						align : 'center'
					}, {
						id : 'Password',
						header : '密码',
						sortable : true,
						dataIndex : 'password',
						align : 'center'
					},{
						id : 'Progress',
						header : 'Progress',
						sortable : true,
						dataIndex : 'progress',
						align : 'center'
					}]

				});

				var migration_panel = new Ext.Panel({
					frame : true,
					layout : 'border',
					region : 'center',
					title : '迁移面板',
					height : 250,
					items : [w_3, w_4]
				});

				////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

				var proxy = new Ext.data.HttpProxy({
					url : 'datamigration.svt?kind=datasource'
				});
				//定义reader
				var reader = new Ext.data.JsonReader({}, [{
					name : 'userid'
				}, {
					name : 'username'
				}, {
					name : 'password'
				}, {
					name : 'url'
				}, {
					name : 'datasourceid'
				}, {
					name : 'vender'
				}, {
					name : 'schemaname',
				}, {
					name : 'state'
				}, {
					name : 'driver'
				}]);
				//构建Store   
				var store = new Ext.data.Store({
					proxy : proxy,
					reader : reader
				});
				//载入
				store.load();

				//////////////////////////////////////Migration panel

				var dataSources = new Ext.grid.GridPanel(
						{
							height : 250,
							store : store,
							frame : true,
							region : 'north',
							store : store,
							margins : '5 5 0 5',
							tbar : [
									new Ext.Button(
											{
												text : '启动',
												width : 70,
												handler : function() {

													var requestConfig = {
														url : 'datamigration.svt?kind=migrate&tablename='
																+ a_tablename,
														params : {
															username : a_username,
															password : a_password,
															url : a_url,
															driver : a_driver,
															tablename : a_tablename
														},
														callback : function(
																option,
																success,
																response) {
															if (response.responseText == "0") {
																Ext.MessageBox
																		.alert(
																				"Warn!",
																				"Please, choose a Table to migrate !");
															} else if (response.responseText == "1") {
																Ext.MessageBox
																		.alert(
																				"Prompt!",
																				"Begin migration.",
																				function(
																						obj) {
																					if (obj == "ok") {
																						Ext
																								.getCmp(
																										"window")
																								.close();
																						w_4
																								.getStore()
																								.reload();
																					}
																				});

															} else if (response.responseText == "2") {
																Ext.MessageBox
																		.alert(
																				"Warning!",
																				"Don't allow to do migration repeatedly!")
															}
														}
													}
													Ext.Ajax
															.request(requestConfig);

												}
											}),
									{
										xtype : 'tbseparator'
									},
									// 									new Ext.Button({
									// 										text : 'Stop Migrate',
									// 										width : 70
									// 									}),
									{
										xtype : 'tbseparator'
									},
									new Ext.Button(
											{
												text : '删除',
												width : 70,
												handler : function() {
													var requestConfig = {
														url : 'datamigration.svt?kind=delete',
														params : {
															username : a_username,
															password : a_password,
															url : a_url,
															driver : a_driver
														},
														callback : function(
																option,
																success,
																response) {
															var msg = [
																	success,
																	option.customer,
																	response.responseText];
// 															alert(msg);
															//grid.getStore().reload();
														}
													}
													Ext.Ajax
															.request(requestConfig);
															dataSources.getStore().reload();
												}
											}),
									{
										xtype : 'tbseparator'
									},
									new Ext.Button(
											{
												text : '测试数据库链接',
												width : 70,
												handler : function() {

													var requestConfig = {
														url : 'datamigration.svt?kind=test',
														params : {
															username : a_username,
															password : a_password,
															url : a_url,
															driver : a_driver
														},
														callback : function(
																option,
																success,
																response) {
															if (response.responseText == "1") {
																Ext.MessageBox
																		.alert(
																				"Prompt",
																				"当前数据源可用.");
// 																alert(dataSources
// 																		.getSelectionModel()
// 																		.getSelections()[0]
// 																		.get("state").value);
															} else {
																Ext.MessageBox
																		.alert(
																				"Warn!",
																				"Invalidate data source, please check!");
															}
														}
													}
													Ext.Ajax
															.request(requestConfig);

												}
											}),
									{
										xtype : 'tbseparator'
									},
									new Ext.Button(
											{
												text : '表信息',
												width : 70,
												handler : function() {
													if (window == "null") {
														Ext.MessageBox
																.alert(
																		"Prompt",
																		"Please choose a data source.");
													}
													window.show();
													window = "null";
												}
											})],
							columns : [/*{
								id : 'Data Source Id',
								header : 'datasourceid',
								width : 160,
								sortable : true,
								dataIndex : 'datasourceid'
							},*/ {
								id : 'User Name',
								header : '用户名',
								width : 160,
								sortable : true,
								dataIndex : 'username'
							}, {
								id : 'Password',
								header : '密码',
								width : 160,
								sortable : true,
								dataIndex : 'password'
							}, {
								id : 'Connection URL',
								header : '链接URL字符串',
								width : 160,
								sortable : true,
								dataIndex : 'url'
							}, {
								id : 'Driver',
								header : '驱动',
								width : 160,
								sortable : true,
								dataIndex : 'driver'
							}, {
								id : '数据库',
								header : '数据库类型',
								width : 160,
								sortable : true,
								dataIndex : 'vender'
							}, {
								id : 'Instance Name',
								header : '实例名',
								width : 160,
								sortable : true,
								dataIndex : 'schemaname'
							}, {
								id : 'State',
								header : '状态',
								width : 160,
								sortable : true,
								dataIndex : 'state'
							}],
							stripeRows : true,
							title : '数据源',
							// config options for stateful behavior
							stateful : true,
							stateId : 'grid'
						});

				var viewport = new Ext.Viewport({
					layout : 'border',
					items : [dataSources, migration_panel]
				});

				/***********************************************
				
							Listener
					
				 ***********************************************/
				dataSources.addListener('cellclick', cellclick);

				function cellclick(grid, rowIndex, columnIndex, e) {
					var record = grid.getStore().getAt(rowIndex); //Get the Record   
					var selectRows = grid.getSelectionModel().getSelections();
					a_username = selectRows[0].get("username");
					a_url = selectRows[0].get("url");
					a_password = selectRows[0].get("password");
					a_driver = selectRows[0].get("driver");

					/***********************************************
							  Window Panel
					 ***********************************************/

					var proxy_1 = new Ext.data.HttpProxy({
						url : 'datamigration.svt?kind=column&username='
								+ a_username + '&url=' + a_url + '&password='
								+ a_password + '&driver=' + a_driver
								+ '&schemaname=' + a_schemaname + '&tablename='
								+ a_tablename
					});

					var reader_1 = new Ext.data.JsonReader({}, [{
						name : 'columnname'
					}, {
						name : 'pk'
					}, {
						name : 'fk'
					}, {
						name : 'referenceto'
					}]);

					var store_1 = new Ext.data.Store({
						proxy : proxy_1,
						reader : reader_1
					});

					store_1.load();

					var win_1 = new Ext.grid.GridPanel({
						store : store_1,
						margins : '5 5 5 0',
						title : '列',
						frame : true,
						width : 300,
						region : 'east',
						columns : [{
							id : '列名',
							header : '列名',
							//width : 90,
							sortable : true,
							dataIndex : 'columnname',
							align : 'center'
						}, {
							id : 'Primary Key',
							header : 'PK',
							//width : 60,
							sortable : true,
							dataIndex : 'pk',
							align : 'center'
						}, {
							id : 'Foreign Key',
							header : 'FK',
							//width : 60,
							sortable : true,
							dataIndex : 'fk',
							align : 'center'
						}
						/* 						, {
						 // 							id : 'Reference To',
						 // 							header : 'referenceto',
						 width : 90,
						 // 							sortable : true,
						 // 							dataIndex : 'referenceto',
						 // 							align : 'center'
						 // 						}*/
						]
					});

					var proxy_2 = new Ext.data.HttpProxy({
						url : 'datamigration.svt?kind=table&username='
								+ a_username + '&url=' + a_url + '&password='
								+ a_password + '&driver=' + a_driver
					});

					var reader_2 = new Ext.data.JsonReader({}, [{
						name : 'schemaname'
					}, {
						name : 'tablename'
					}, {
						name : 'rowcount'
					}, {
						name : 'columncount'
					}]);

					var store_2 = new Ext.data.Store({
						proxy : proxy_2,
						reader : reader_2
					});

					store_2.load();

					var win_2 = new Ext.grid.GridPanel({
						store : store_2,
						region : 'center',
						title : '数据库表',
						frame : true,
						margins : '5 5 5 5',
						columns : [{
							id : 'schemaname',
							header : '实例名',
							//width : 90,
							sortable : true,
							dataIndex : 'schemaname',
							align : 'center'
						}, {
							id : '表名',
							header : '表名',
							//width : 35,
							sortable : true,
							dataIndex : 'tablename',
							align : 'center'
						}, {
							id : '行数',
							header : '行数',
							//width : 35,
							sortable : true,
							dataIndex : 'rowcount',
							align : 'center'
						}, {
							id : 'columncount',
							header : '列数',
							//width : 35,
							sortable : true,
							dataIndex : 'columncount',
							align : 'center'
						}]
					});

					/////////////////////////////////////////////////////////////////////////////////
					//				win_2(tables) listener
					/////////////////////////////////////////////////////////////////////////////////

					win_2.addListener('cellclick', win_2_cellclick);

					function columnSelet() {
						win_1.getStore().proxy.conn.url = 'datamigration.svt?kind=column&username='
								+ a_username
								+ '&url='
								+ a_url
								+ '&password='
								+ a_password
								+ '&driver='
								+ a_driver
								+ '&schemaname='
								+ a_schemaname
								+ '&tablename='
								+ a_tablename;
						win_1.getStore().reload();
					}

					function win_2_cellclick(grid, rowIndex, columnIndex, e) {
						var record = grid.getStore().getAt(rowIndex); //Get the Record   
						var selectRows = grid.getSelectionModel()
								.getSelections();
						a_schemaname = selectRows[0].get("schemaname");
						a_tablename = selectRows[0].get("tablename");
						columnSelet();
					}

					// 			alert(a_username + a_url + a_password + a_driver);

					window = new Ext.Window({
						id : 'window',
						title : '数据源详情',
						plain : true,
						closable : true,
						layout : 'border',
						height : 350,
						width : 650,
						items : [win_1, win_2]
					});

					// 			window.add(winPanel);
					//window.show();

				}
			});
</script>
</head>

<body>
</body>
</html>
